﻿using System.Collections.Generic;
using PSE.Updater.Common.Providers;
using PSE.Updater.Common.Providers.Interfaces;
using System;

namespace PSE.Updater.Server.Engine
{
    /// <summary>
    /// Classe Factory para criação de Providers
    /// </summary>
    public sealed class ProviderFactory
    {
        #region [ Singleton ]
        private static object _singleLock = new object();
        private static ProviderFactory _instance;
        private ProviderFactory() { }
        public static ProviderFactory Instance
        {
            get
            {
                lock (_singleLock)
                {
                    if (_instance == null)
                        _instance = new ProviderFactory();

                    return _instance;
                }
            }
        }
        #endregion [ Singleton ]

        /// <summary>
        /// Cria um Objeto Arquivo de Detalhes
        /// </summary>
        /// <param name="productName">Nome do produto</param>
        /// <returns>Objeto Arquivo de Detalhes</returns>
        public IFileDetails CreateFileDetails(ProviderType type, string productName, string filePath, string rootDirectoryWatcher)
        {
            IFileDetails file = null;
            switch (type)
            {
                case ProviderType.Updates:
                    file = new PSE.Updater.Server.Engine.Providers.Updates.FileDetails(productName);
                    break;
                case ProviderType.ConfigurationFile:
                    file = new PSE.Updater.Server.Engine.Providers.ConfigurationFile.FileDetails(productName, rootDirectoryWatcher);
                    break;
            }
            file.Initialize(filePath, type);
            return file;
        }

        /// <summary>
        /// Cria o Objeto UpdateDiscovery
        /// </summary>
        /// <returns>Objeto UpdateDiscovery</returns>
        public IUpdateDiscovery GetUpdateDiscovery(ProviderType type)
        {
            switch (type)
            {
                case ProviderType.Updates:
                    return PSE.Updater.Server.Engine.Providers.Updates.UpdateDiscovery.Instance;
                case ProviderType.ConfigurationFile:
                    return PSE.Updater.Server.Engine.Providers.ConfigurationFile.UpdateDiscovery.Instance;
            }

            throw new ArgumentNullException("type");
        }
    }
}
